//
//  YKModifyPasswordViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKModifyPasswordViewController.h"
#import <TFTextFieldWithTitleItem.h>
#import "YKTabBarViewController.h"
#import "YKPopupView.h"
@interface YKModifyPasswordViewController ()

@end

@implementation YKModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"修改密码";
    titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(titleLabel.mas_bottom).offset(5);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    TFTableViewSection *section = [TFTableViewSection section];
    section.headerHeight = .1f;
    section.footerHeight = .1f;
    TFTextFieldWithTitleItem *oldPasswordItem = [TFTextFieldWithTitleItem itemWithTitle:@"旧密码" placeholder:@"请输入旧密码" value:nil enabled:YES selectionHandler:nil clickHandler:^(__kindof TFTableViewItem *item, NSInteger actionType, id sender) {
        
    }];
    oldPasswordItem.secureTextEntry = YES;
    [section addItem:oldPasswordItem];
    
    TFTextFieldWithTitleItem *newPasswordItem = [TFTextFieldWithTitleItem itemWithTitle:@"新密码" placeholder:@"请输入新密码" value:nil enabled:YES selectionHandler:nil clickHandler:^(__kindof TFTableViewItem *item, NSInteger actionType, id sender) {
        
    }];
    newPasswordItem.secureTextEntry = YES;
    [section addItem:newPasswordItem];
    
    TFTextFieldWithTitleItem *newPassword2Item = [TFTextFieldWithTitleItem itemWithTitle:@"新密码" placeholder:@"请再次输入新密码" value:nil enabled:YES selectionHandler:nil clickHandler:^(__kindof TFTableViewItem *item, NSInteger actionType, id sender) {
        
    }];
    newPassword2Item.secureTextEntry = YES;
    [section addItem:newPassword2Item];
    
    [self.tableViewManager addSection:section];
    
    UIButton* confirmButton = [[UIButton alloc]init];
    [confirmButton setTitle:@"确认修改" forState:UIControlStateNormal];
    [confirmButton setBackgroundImage:TFSTYLE(navigationBarBackgroudImage) forState:UIControlStateNormal];
    
    [confirmButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"#999999"]] forState:UIControlStateDisabled];
    
    [confirmButton setContentEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    confirmButton.tf_cornerRadius = 4.0f;
    
    [self.view addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(15);
        make.trailing.equalTo(self.view.mas_trailing).offset(-15);
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
    }];
    
    confirmButton.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        YKTabBarViewController *tab = [YKTabBarViewController sharedInstance];
        if (newPasswordItem.value.length > 0 && [newPasswordItem.value isEqualToString:newPassword2Item.value] && oldPasswordItem.value.length > 0) {
            [TFHTTPMANAER POST:@"personalCentr/doEdtPasswordById.htm" parameters:@{@"id":tab.userModel.ID, @"user_id":tab.userModel.USER_ID, @"oldPassword":oldPasswordItem.value,@"newPassword":newPassword2Item.value} HUD:@"修改中" success:^(id responseObject) {
//                [self.navigationController popViewControllerAnimated:YES];
                [YKPopupView dismissViewController:self];
                [self showHint:@"修改成功"];
            } failure:^(id responseObject, NSError *error) {
                [self showHint:error.localizedFailureReason];
            }];
        }
        else {
            if (newPassword2Item.value.length == 0 || newPasswordItem.value.length == 0 || oldPasswordItem.value.length == 0) {
                [self showHint:@"有空没有填写"];
            }
            else {
                if (![newPassword2Item.value isEqualToString:newPasswordItem.value]) {
                    [self showHint:@"两次新密码填写不一致,请重试"];
                }
            }
        }
        return [RACSignal empty];
    }];
    
    
}



@end
